﻿using System;
using System.Net;
using ETModel;
using UnityEngine;
using UnityEngine.UI;

namespace ETHotfix
{
	[ObjectSystem]
	public class UiLoginComponentSystem : AwakeSystem<UILoginComponent>
	{
		public override void Awake(UILoginComponent self)
		{
			self.Awake();
		}
	}
	
	public class UILoginComponent: Component
	{
		private InputField account;
        private InputField password;
        private Text prompt;

        public void Awake()
		{
			ReferenceCollector rc = this.GetParent<UI>().GameObject.GetComponent<ReferenceCollector>();

            this.account = rc.Get<GameObject>("Account").GetComponent<InputField>();
            this.password = rc.Get<GameObject>("Password").GetComponent<InputField>();
            this.prompt = rc.Get<GameObject>("Prompt").GetComponent<Text>();

            rc.Get<GameObject>("LoginButton").GetComponent<Button>().onClick.Add(OnLogin);
            rc.Get<GameObject>("RegisterButton").GetComponent<Button>().onClick.Add(OnRegister);

            rc.Get<GameObject>("HotfixPrompt").GetComponent<Text>().text = "勇者斗地主";
        }

        public void SetPrompt(string str)
        {
            this.prompt.text = str;
        }

        private void OnLogin()
		{
            LoginHelper.OnLoginAsync(account.text, password.text).Coroutine();
        }

        private void OnRegister()
        {
            LoginHelper.OnRegisterAsync(account.text, password.text).Coroutine();
        }
    }
}
